package cn.iocoder.yudao.module.member.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
import cn.iocoder.yudao.module.member.dal.redis.no.OrderNoRedisDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信机器人
 *
 */
@Slf4j
public class RobbleWXUtil {

//    private static String url = "https://developer.work.weixin.qq.com/document/path/91770";
    private static final String SITE_CONFIG_CACHE_WECHATROBOT = "SITE:CONFIG:CACHE:WECHATROBOT";
//    private static String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=92c421e1-61a3-46ac-b8cc-64c1db261ab3";
//    private static final String SITE_CONFIG_CACHE_WECHATROBOT = "SITE:CONFIG:CACHE:WECHATROBOT";
    public static void  sendMessage( String context) {
        MsgVo msgVo = new MsgVo();
        msgVo.setMsgtype("markdown");
        TextVo textVo = new TextVo();
        textVo.setContent(context);
        msgVo.setMarkdown(textVo);
        StringRedisTemplate bean = SpringUtils.getBean(StringRedisTemplate.class);
       String  url =  bean.opsForValue().get(SITE_CONFIG_CACHE_WECHATROBOT);
        log.info("微信机器消息发送参数{}",JSONUtil.toJsonStr(msgVo));
        try {
            if (StrUtil.isEmpty(url)){
                log.info("未找到机器人配置");
            return;
            }
            HttpUtil.createPost(url).header("Content-Type", "application/json").body(JSONUtil.toJsonStr(msgVo)).execute();
        }catch (Exception e){
           log.info("微信机器消息发送失败{}",e.getMessage());
        }

    }

    public static void main(String[] args) {
        sendMessage("新出秒出证书<font color=\\\"warning\\\"> "+ 111 +"</font>,\\n\n" +
                "         >编号:<font color=\\\"comment\\\"> "+ 222 +  "</font>\n" +
                "         >用户:<font color=\\\"comment\\\"> "+ 333 +  "</font>\n" +
                "         >余额:<font color=\\\"comment\\\"> "+ 444 +  "</font>\n" +
                "         >版本:<font color=\\\"comment\\\"> "+ 555 +  "</font>\n" +
                "         >平台:<font color=\\\"comment\\\"> "+ "本站" +  "</font>\n" +
                "         >名称:<font color=\\\"comment\\\"> "+ 666 +  "</font>\n" +
                "         >设备:<font color=\\\"comment\\\"> "+ 777 +  "</font>\n" +
                "         >udid:<font color=\\\"comment\\\"> "+ 888 +  "</font>\n" +
                "         >操作:<font color=\\\"comment\\\">" + 999 +  "</font>"    );
    }
}
